<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px;">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6" >
              <a-form-item label="文件名称">
                <a-input v-model="queryParam.fileName" placeholder="请输入文件名称" allow-clear @keyup.enter.native="handleQuery"/>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <advance-table
        title="对象存储"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
        tableKey="base-oss-index-table"
        @change="handleTableChange"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="ossList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <div class="table-operations" slot="button">
          <a-upload
              name="file"
              :multiple="false"
              :action="uploadUrl"
              :headers="headers"
              :showUploadList="false"
              @change="handleQuery"
            >
            <a-button> <a-icon type="upload" />上传</a-button>
          </a-upload>
          <a-button type="danger" v-if="!multiple" :disabled="multiple" @click="handleDelete" style="margin-left:8px;">
            <a-icon type="delete" />删除
          </a-button>
        </div>
        <span slot="size" slot-scope="{text, record}">{{formatFileSize(text)}}</span>
        <span slot="operation" slot-scope="{text, record}">
          <a @click="handlePreview(record)">
            预览
          </a>
          <a-divider type="vertical"/>
          <a @click="handleDownload(record)">
            下载
          </a>
          <a-divider type="vertical"/>
          <a @click="handleDelete(record)">
            删除
          </a>
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import { listSysOssFile, delSysOssFile } from '@/api/system/oss'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import { formatFileSize } from '@/utils/util'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { Base64 } from 'js-base64'
export default {
  name: 'SysOssFile',
  components: {
    AdvanceTable
  },
  data () {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + '/system/oss/upload',
      headers: { 'Authorization': 'Bearer ' + storage.get(ACCESS_TOKEN) },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 对象存储表格数据
      ossList: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        fileName: undefined,

        url: undefined

      },
      columns: [
        {
          title: '文件名称',
          dataIndex: 'fileName',
          ellipsis: true,
          align: 'left',
          width: '40%'
        },
        {
          title: '文件大小',
          dataIndex: 'size',
          ellipsis: true,
          align: 'left',
          width: '20%',
          scopedSlots: { customRender: 'size' }
        },
        {
          title: '文件类型',
          dataIndex: 'contentType',
          ellipsis: true,
          align: 'left',
          width: '20%'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '20%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created () {
    this.getList()
  },
  methods: {
    formatFileSize(size){
      return formatFileSize(size)
    },
    /** 查询对象存储列表 */
    getList () {
      this.loading = true
      listSysOssFile(this.queryParam).then(response => {
        this.ossList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        fileName: undefined,

        url: undefined

      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange (current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handlePreview (row) {
      window.open(process.env.VUE_APP_ONLINE_BASE_URL + '?url=' + encodeURIComponent(Base64.encode(row.url)), '_blank')
    },
    handleDownload (row) {
      window.open(row.url, '_blank')
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const ossIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk () {
          return delSysOssFile(ossIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    handleTableChange (pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    }
  }
}
</script>
